package org.dromara.solonplugins.bee;

import javax.sql.DataSource;

import org.dromara.solonplugins.bee.integration.BeePropertiesManager;
import org.noear.solon.core.AopContext;
import org.noear.solon.core.Plugin;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.BeeFactory;
import org.teasoft.honey.osql.core.SessionFactory;

/**
 * Solon插件入口
 *
 * @author: xls-web (小xu中年)
 */
public class XPluginImp implements Plugin {
    @Override
    public void start(AopContext context) throws Throwable {
    	BeePropertiesManager bm = new BeePropertiesManager(context);
    	
        
    	bm.getBean();//初始化和ManageConfig相关的各种bean
    	bm.xmlBean();//初始化数据库操作层
    	
    	//监听DataSource注入事件
        context.subWrapsOfType(DataSource.class, bw->{
        	//注入BeeFactory
        	BeeFactory beeFactory = BeeFactory.getInstance();
    		beeFactory.setDataSource(bw.raw());
    		context.wrapAndPut(BeeFactory.class, beeFactory);
    		
    		//注入SessionFactory
    		SessionFactory factory = new SessionFactory();
    		factory.setBeeFactory(beeFactory);
    		context.wrapAndPut(SessionFactory.class, factory);
        });
    }
}
